package com.kgcx.order.rabbitmq;

import com.kgcx.core.utils.JsonUtil;
import com.kgcx.order.domain.Order;
import com.kgcx.order.service.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * Created by zhulong on 17/11/13.
 * 订单持久化监听
 */
@Component

public class OrderPersistenceListener {

    private static final Logger LOG = LoggerFactory.getLogger(OrderPersistenceListener.class);
    @Autowired
    private OrderService orderService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @RabbitListener(queues = "kgcxOrderPersistenceQueue", errorHandler = "custRabbitListenerErrorHandler")
    public void processOrderStatus(String json) {
        LOG.info("订单 - 开始入库，json=" + json);

        Order item = JsonUtil.toObject(json, Order.class);

        if (item != null) {
            if (orderService.getByOrderNo(item.getOrderNo()) == null) {
                orderService.add(item);
            }
        }
    }
}
